<template>
  <div class="register">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="username" name="username" label="用户名" placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" clearable />
        <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"  clearable/>
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit" color="#09BB07">
          注册
        </van-button>
        <van-button round block style="margin-top: 8px;" type="primary" color="#09BB07" plain  @click="handleReset">
          重置
        </van-button>
      </div>
    </van-form>
    <div style="display: flex;justify-content: center;">
      <router-link to="/login" style="color: #09BB07;">已有账号？点击登录</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref} from 'vue';
import { showToast } from 'vant'
import { register } from '@/apis/auth'

const username = ref('');
const password = ref('');
const onSubmit = async (values) => {
  const {code,msg=''} =  await register({
    ...values,
  });
  if (code !== 200) {
    showToast(msg);
    return;
  }
  if (code === 200) {
    showToast('注册成功');
    return;
  }

};

const handleReset = () => {
  username.value = '';
  password.value = '';
};

</script>

<style lang="less" scoped>
.register {
  margin-top: 48px;
}
</style>
